package org.simple.learning.spring.boot.aliyun.oss.service;

import com.aliyun.oss.OSSClient;
import org.simple.learning.spring.boot.aliyun.oss.config.OssConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * 项目名称：java-web-leaning
 * 类名称：OssService
 * 类描述：OssService
 * 创建时间：2018/9/4
 *
 * @author guihuo   (E-mail:1620657419@qq.com)
 * @version v1.0
 */

public class OssService {

    @Autowired
    private OSSClient ossClient;

    @Autowired
    private OssConfig ossConfig;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 将本地文件上传到OSS
     *
     * @param file 待上传文件
     * @param dir  OSS 文件路径
     * @return .
     */
    public String upload(File file, String dir) {
        return upload(file, dir, file.getName());
    }

    /**
     * 将本地文件上传到OSS
     *
     * @param file     待上传文件
     * @param dir      OSS 文件路径
     * @param fileName OSS 文件名
     * @return .
     */
    public String upload(File file, String dir, String fileName) {
        String objKey = dir + "/" + fileName;
        ossClient.putObject(ossConfig.getBucketName(), objKey, file);
        return ossFilePath() + objKey;
    }

    /**
     * 将输入流上传到OSS
     *
     * @param input    待上传流
     * @param dir      OSS 文件路径
     * @param fileName OSS 文件名
     * @return .
     */
    public String upload(InputStream input, String dir, String fileName) {
        String objKey = dir + "/" + fileName;
        ossClient.putObject(ossConfig.getBucketName(), objKey, input);
        return ossFilePath() + objKey;
    }

    /**
     * 将网络文件上传到OSS
     *
     * @param urlPath URL资源
     * @param dir     OSS 文件路径
     * @return .
     */
    public String upload(String urlPath, String dir) {
        return upload(urlPath, dir, urlPath.substring(urlPath.lastIndexOf("/") + 1));
    }

    /**
     * 将网络文件上传到OSS
     *
     * @param urlPath  URL资源
     * @param dir      OSS 文件路径
     * @param fileName OSS 文件名
     * @return .
     */
    public String upload(String urlPath, String dir, String fileName) {
        String objKey = dir + "/" + fileName;

        try {
            URL url = new URL(urlPath);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            int result = httpURLConnection.getResponseCode();

            if (result == HttpURLConnection.HTTP_OK) {
                ossClient.putObject(ossConfig.getBucketName(), objKey, urlConnection.getInputStream());
            }
        } catch (FileNotFoundException e) {
            log.error("文件不存在：{}", e.getStackTrace());
        } catch (IOException e) {
            log.error("IO异常：{}", e.getStackTrace());
        }
        return ossFilePath() + objKey;
    }

    private String ossFilePath() {
        String protocol = "http://";
        String endPoint = ossConfig.getEndpoint().substring(protocol.length());
        return protocol + ossConfig.getBucketName() + "." + endPoint + "/";
    }


}
